<?php
namespace Forxx;
use \Lavender\Errno;
use \Lavender\Exception;
use \Forxx\Signature;
use \Forxx\ErrnoPlus;
use \Lavender\Filter;
use \Lavender\Core;

class ForxxWebService extends \Lavender\WebService
{

    public function filter_input($rules,$required = true){
        return  $this->parameters($rules,self::M_RAW|self::M_JSON,$required);
    }

	protected function get_request_param()
	{
		$request = array();
		if (!empty($_GET)) {
			foreach ($_GET as $key => $value) {
				$request[$key] = $value;
			}
		}
		if (!empty($_POST)) {
			foreach ($_POST as $key => $value) {
				$request[$key] = $value;
			}
		}
        $input = Static::get_raw_body();
        if(!empty($input))
        {
            $request['_input'] = $input;
        }
        return $request;
	}

    public function log($contents, $log_name = 'debug')
    {
        $old_mask = umask(0);

        //check & make dir
        $dir = L_WORKSPACE_PATH . 'log/' . L_APP_NAME . '/';
        if (!is_dir($dir)) {
            mkdir($dir, 0777, true);
        }

        //write file
        $file = $dir . $log_name;
        file_put_contents($file . '.log', $contents, FILE_APPEND | LOCK_EX);

        //keep small than 1G
        if (filesize($file . '.log') > 1000000000) {
            rename($file . '.log', $file . '.' . date('His') . '.log');
        }
        umask($old_mask);
    }
}
